package com.atstudy.dt;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/16 10:02
 * @copyright: 博为峰
 */
public class Test {
    public static void main(String[] args) {
        Student stu01 = new Student();
//        stu01.eat();
//        stu01.study();

        //多态
//        Person stu02 = new Student();//向上转型（类型提升）
//        stu02.eat();
//        stu02.study(); 不能再使用子类特有的方法

//        Student stu03 = (Student)stu02;//向下转型（强制类型转换）
//        stu03.study();

//        m1(stu01);
        m3(stu01);

        Worker worker = new Worker();

//        m2(worker);
        m3(worker);

    }

    //解耦（高内聚，低耦合）
    //instanceof 类类型判断
    public static void m3(Person p) {//Person p = stu01
        p.eat();
        if (p instanceof Student) {
            Student stu = (Student) p;
            stu.study();
        }
//        p.study();
        if (p instanceof Worker) {
            Worker worker = (Worker) p;//ClassCastException 类型转换异常
            worker.work();
        }
//        p.work();
    }

    public static void m1(Student student) {
        student.eat();
    }

    public static void m2(Worker worker) {
        worker.eat();
    }
}
